package org.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.system.common.enums.OrderStatusEnum;
import org.example.system.common.enums.PayTypeEnum;
import org.example.system.common.exception.BusinessException;
import org.example.system.common.result.Result;
import org.example.system.common.util.IPUtils;
import org.example.system.common.util.OrderSnGenerator;
import org.example.system.common.util.WxPayUtils;
import org.example.system.config.WechatConfig;
import org.example.system.model.entity.BizOrder;
import org.example.system.model.form.OrderForm;
import org.example.system.service.BizOrderService;
import org.example.system.service.PaymentStrategy;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

@Service
public class WeChatPayStrategy implements PaymentStrategy {


    @Resource
    private BizOrderService orderService;

    @Resource
    private WxPayService wxPayService;

    @Resource
    private WechatConfig wechatConfig;


    @Override
    public Result prePay(String orderSn, HttpServletRequest request) throws WxPayException {
        BizOrder order = orderService.getOrderByOrderSn(orderSn);
        if (order == null) {
            throw new BusinessException("此订单不存在");
        }
        // 微信支付请求参数
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        // 随机字符串
        orderRequest.setNonceStr(WxPayUtils.generateNonceStr());
        // 订单流水号
        orderRequest.setOutTradeNo(order.getOutTradeNo());
        //元转成分 BaseWxPayRequest.yuanToFen(order.getTotalFee().toString())
        Integer totalFee = BaseWxPayRequest.yuanToFen(order.getTotalFee().toString());
        orderRequest.setTotalFee(1); // 为了测试，订单总金额设置为1分钱。
        //商品简单描述 "必填字段【[body]】必须提供值！"
        orderRequest.setBody("荣达租车");
        //交易类型 小程序取值如下：JSAPI
        orderRequest.setTradeType(WxPayConstants.TradeType.JSAPI);
        // 微信用户唯一标识 openid
        orderRequest.setOpenid(order.getOpenid());
        //异步支付通知地址
        orderRequest.setNotifyUrl(wechatConfig.getNotifyUrl());
        // 签名类型  MD5/HMAC-SHA256
        orderRequest.setSignType(WxPayConstants.SignType.MD5);
        // 终端IP地址
        String ip = IPUtils.getIpAddr(request);
        orderRequest.setSpbillCreateIp(ip);
        try {
            Object preOrder = wxPayService.createOrder(orderRequest);
            return Result.success(preOrder);// 返回前端是预支付订单
        }catch (WxPayException ex) {
            return Result.failed(ex.getErrCodeDes());
        }
    }


    @Override
    public PayTypeEnum getPayChannel() throws IllegalArgumentException{
        return PayTypeEnum.WECHAT_PAY;
    }
}
